<template>
  <div class="tab-bar-item" @click="itemClick">
    <div v-if="!isactive"><slot name="item-icon"></slot></div>
    <div v-else><slot name="item-icon-active"></slot></div>
    <div :style="activeStyle"><slot name="item-text"></slot></div>
    <!-- :style 动态绑定样式 -->
    <!-- 由于slot插槽在使用时回整个被替换掉 里面的属性类等会被替换掉 
    因此在slot外包一层div 并将属性等条件放在div中 这样不会被替换掉-->
  </div>
</template>

<script>
export default {
  props: {
    path: String,
    activitycolor: {
      type: String,
      default: "red"
    }
  },
  computed: {
    isactive() {
      return this.$route.path.indexOf(this.path) != -1;
    },
    // 决定当前路径是否活跃
    activeStyle() {
      return this.isactive ? { color: this.activitycolor } : {};
    }
    // 用来决定活跃的样式 动态获取由appvue传进来的属性值
  },
  methods: {
    itemClick() {
      this.$router.push(this.path).catch(err => {});
    }
  }
};
</script>

<style>
.tab-bar-item {
  flex: 1;
  text-align: center;
}
.tab-bar-item img {
  width: 24px;
  height: 24px;
  margin-top: 3px;
}
.activetext {
  color: red;
}
</style>
